import Skeleton, { SkeletonTheme } from "react-loading-skeleton"; import "react-loading-skeleton/dist/skeleton.css"; import Head from "next/head"; import Image from "next/image"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import Layout from "../../components/layout"; import Link from "next/link"; import Content from "../../components/hero/content"; import { useSession } from "next-auth/react"; const query = ` query ($username: String, $status: MediaListStatus) { MediaListCollection(userName: $username, type: ANIME, status: $status, sort: SCORE_DESC) { user { id name about (asHtml: true) createdAt avatar { large } statistics { anime { count episodesWatched meanScore minutesWatched } } bannerImage mediaListOptions { animeList { sectionOrder } } } lists { status name entries { id mediaId status progress score media { id status title { english romaji } episodes coverImage { large } } } } } } `; export default function Info() { const { data: session, status } = useSession(); const [data, setData] = useState(null); const [episode, setEpisode] = useState(null); const [loading, setLoading] = useState(false); const [progress, setProgress] = useState(null); const [statuses, setStatuses] = useState(null); const [stall, setStall] = useState(false); const [color, setColor] = useState(null); const [showAll, setShowAll] = useState(false); const [time, setTime] = useState(0); const { id } = useRouter().query; // console.log(stall); useEffect(() => { const defaultState = { data: null, episode: null, loading: true, statuses: null, progress: null, }; // Reset all state variables to their default values Object.keys(defaultState).forEach((key) => { const value = defaultState[key]; if (Array.isArray(value)) { value.length ? eval( `set${ key.charAt(0).toUpperCase() + key.slice(1) }(${JSON.stringify(value)})` ) : eval(`set${key.charAt(0).toUpperCase() + key.slice(1)}([])`); } else { eval( `set${key.charAt(0).toUpperCase() + key.slice(1)}(${JSON.stringify( value )})` ); } }); async function fetchData() { if (id) { setLoading(false); try { const res = await fetch( `https://api.moopa.my.id/meta/anilist/info/${id?.[0]}` ); const data = await res.json(); if (data.episodes.length === 0) { const res = await fetch( `https://api.consumet.org/meta/anilist/info/${id[0]}?provider=9anime` ); const datas = await res.json(); setColor({ backgroundColor: `${data?.color || "white"}` }); setStall(true); setEpisode(datas.episodes); } else { setEpisode(data.episodes); } setColor({ backgroundColor: `${data?.color || "white"}` }); if (session?.user?.name) { const response = await fetch("https://graphql.anilist.co/", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ query: query, variables: { username: session?.user?.name, }, }), }); const dat = await response.json(); const prog = dat.data.MediaListCollection; const gat = prog.lists.map((item) => item.entries); const git = gat.map((item) => item.find((item) => item.media.id === parseInt(data?.id)) ); const gut = git?.find( (item) => item?.media.id === parseInt(data?.id) ); if (gut) { setProgress(gut?.progress); if (gut.status === "CURRENT") { setStatuses("Watching"); } else if (gut.status === "PLANNING") { setStatuses("Planned to watch"); } else if (gut.status === "COMPLETED") { setStatuses("Completed"); } else if (gut.status === "DROPPED") { setStatuses("Dropped"); } else if (gut.status === "PAUSED") { setStatuses("Paused"); } else if (gut.status === "REPEATING") { setStatuses("Rewatching"); } } } if (data.nextAiringEpisode) { setTime( convertSecondsToTime(data.nextAiringEpisode.timeUntilAiring) ); } function getBrightness(color) { const rgb = color.match(/\d+/g); return (299 * rgb[0] + 587 * rgb[1] + 114 * rgb[2]) / 1000; } // set the text color based on the background color function setTextColor(element) { const backgroundColor = getComputedStyle(element).backgroundColor; const brightness = getBrightness(backgroundColor); if (brightness < 128) { element.style.color = "#fff"; // white } else { element.style.color = "#000"; // black } } const elements = document.querySelectorAll(".dynamic-text"); elements.forEach((element) => { setTextColor(element); }); setData(data); setLoading(true); } catch (error) { setTimeout(() => { window.location.reload(); }, 1000); } } // setLoading(true); } fetchData(); }, [id, session?.user?.name]); // console.log(episode); return ( <> {data?.title?.romaji || data?.title?.english}
{data && ( banner anime )}
{loading ? ( data && ( <>
poster anime ) ) : ( )}

{loading ? ( data?.title?.romaji || data?.title?.english ) : ( )}

{loading ? ( data && (
{data?.totalEpisodes} Episodes
{data?.releaseDate}
{data?.rating}%
{data?.type}
{data?.status}
Sub | EN
{data && data.nextAiringEpisode && (
Ep {data.nextAiringEpisode.episode}: {time}
)}
) ) : ( )}
{loading ? (

) : ( )} {/*

{data.description}

*/}
{data && (
Relations
)} {data?.relations?.length > 3 && (
setShowAll(!showAll)} > {showAll ? "show less" : "show more"}
)}
{loading ? data?.relations && data?.relations .slice(0, showAll ? data?.relations.length : 3) .map((relation, index) => { return (
{relation.id}
{relation.relationType}
{relation.title.romaji}
{relation.type}
); }) : [1, 2, 3].map((item) => (
))}
{data && (

Episodes

)} {statuses && ( <>
{statuses} status
)}
{loading ? ( data && (
{episode ? ( episode.map((episode, index) => { return (

Episode {episode.number}

{episode.title && (

"{episode.title}"

)}
); }) ) : (

No Episodes Available

)}
) ) : ( <> )}
{data && (
)}
); } function convertSecondsToTime(sec) { let days = Math.floor(sec / (3600 * 24)); let hours = Math.floor((sec % (3600 * 24)) / 3600); let minutes = Math.floor((sec % 3600) / 60); let time = ""; if (days > 0) { time += `${days}d `; } if (hours > 0) { time += `${hours}h `; } if (minutes > 0) { time += `${minutes}m `; } return time.trim(); }